const app = getApp()
const api = app.globalData.api
Page({
    data: {
        result: [],
        total: 0,
        count: 0,
        page: 1,
        pageAll: 0,
        where: '',
        sortShow: false,
        sortArray: [],
        popNum: 0,
        popShow: false,
        popArray: {}
    },
    list: async function (param = {}, page = 1) {
        const url = '?cla=processReport&fun=getMaterial&page=' + page
        let res = await api.curlPost(url, param)
        if (res.code === 1) {
            const page = res.data.page
            this.setData({
                page: page.pageNow,
                pageAll: page.pageAll,
                result: res.data.result,
                where: api.arrayToJson(param)
            })
        }
    },
    // 选择物料
    seleMaterial: async function (e) {
        let obj = e.currentTarget.dataset
        if (obj.material) {
            const data = api.jsonToArray(obj.material)
            if (data.sendId) {
                wx.navigateTo({
                    url: 'add?data=' + obj.material
                })
            }
        }
    },
    onLoad: async function (options) {
        await this.list()
    },
    onSearch: async function (e) {
        await this.list({
            'wd': e.detail
        })
    },
    // 上拉加载
    onReachBottom() {
        if (!this.loading && this.data.page < this.data.pageAll) {
            const page = parseInt(this.data.page),
                param = api.jsonToArray(this.data.where)
            this.list(param, page + 1)
        }
    },
    showHide(obj, bool = true) {
        const field = obj.currentTarget.dataset.target
        if (field) {
            this.setData({
                [field]: bool
            })
        }
    },
});